package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
       /* // 获取登录用户
        UserDTO user = UserHolder.getUser();
        blog.setUserId(user.getId());
        // 保存探店博文
        blogService.save(blog);
        // 返回id
        return Result.ok(blog.getId());*/
        return blogService.saveBlog(blog);
    }

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        //fixme:修改点赞功能，业务要求是一个用户对一个blog只能点赞一次---->可以用set集合来充当存点赞的容器
        // set集合中有成员互斥，最好了
        return blogService.likesFunction(id);
        /*blogService.update()
                .setSql("liked = liked + 1").eq("id", id).update();
        return Result.ok();*/

    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
       /* // 根据用户查询
        Page<Blog> page = blogService.query()
                .orderByDesc("liked")
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        // 查询用户
        records.forEach(blog -> {
            Long userId = blog.getUserId();
            User user = userService.getById(userId);
            blog.setName(user.getNickName());
            blog.setIcon(user.getIcon());
        });
        return Result.ok(records);*/
        return blogService.queryHotBlog(current);
    }

    @GetMapping("/{id}")
    public Result showBlog(@PathVariable Long id) {
       return blogService.qureyBlog(id);
    }

    @GetMapping("/likes/{id}")
    public Result showLikesList(@PathVariable("id") Long id) {
        //点赞排行榜业务的请求处理方法----->要实现的业务为：查询并显示出当前blog中点赞时间最早的top5，所以返回一个List<UserDto>
        return blogService.showLikeList(id);
    }

    @GetMapping("/of/user")
    public Result showUserBlog
            (@RequestParam(value = "id")Long id,@RequestParam(value = "current",defaultValue = "1")Integer current){
        return blogService.showUserBlog(id,current);
    }

    @GetMapping("/of/follow")
    public Result followPush
            (@RequestParam(value = "lastId") Long lastId,@RequestParam(value = "offset",defaultValue = "0") Integer offset ){
        //在这里实现从收件箱中（redis中的相应的zset中）获取到关注的用户发送的blog，同时实现滚动分页功能
        return blogService.followPush(lastId,offset);
    }

}
